﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Expressions.Compiler.Internal
{

    using Interfaces;

    internal sealed class ExpressionContext<T> : IExpressionContext<T>
    {
        private readonly Position<T> _begin;
        private readonly Position<T> _end;
        private readonly Entity<T> _entity;
        private readonly string _name;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionContext"/> class.
        /// </summary>
        /// <param name="entity">The token.</param>
        /// <param name="name">The name.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        internal ExpressionContext(Entity<T> entity, string name, Position<T> start, Position<T> end)
        {
            _entity = entity;
            _name = name;
            _begin = start;
            _end = end;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionContext"/> class.
        /// </summary>
        /// <param name="entity">The token.</param>
        /// <param name="name">The name.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        internal ExpressionContext(Entity<T> entity, /*string name,*/ Position<T> start, Position<T> end)
        {
            _entity = entity;
            //_name = name;
            _begin = start;
            _end = end;
        }
#if TRACE
        public string Name
        {
            get
            {
                return _name;
            }
        }
#endif

        /// <summary>
        /// Token
        /// </summary>
        public Entity<T> Token { get { return _entity; } }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Parser<T>.GetText(_begin, _end);
        }
    }

    internal sealed class ExpressionContext : IExpressionContext
    {
        private readonly Position _begin;
        private readonly Position _end;
        private readonly Entity _entity;

#if TRACE   
        private readonly string _name;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionContext"/> class.
        /// </summary>
        /// <param name="entity">The token.</param>
        /// <param name="name">The name.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        internal ExpressionContext(Entity entity, string name, Position start, Position end)
        {
            _entity = entity;
            _name = name;
            _begin = start;
            _end = end;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }


#else
        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionContext"/> class.
        /// </summary>
        /// <param name="entity">The token.</param>
        /// <param name="name">The name.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        internal ExpressionContext(Entity entity, Position start, Position end)
        {
            _entity = entity;
            _begin = start;
            _end = end;
        }
#endif

        /// <summary>
        /// Token
        /// </summary>
        public Entity Token { get { return _entity; } }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Parser.GetText(_begin, _end);
        }
    }
}
